#include <cstdlib>
#include <iostream>
#include <sstream>

using namespace std;

class Vector {

private:
	int x = 0;
	int y = 0;
public:
	Vector(int x,int y)
	{
		this->x = x;
		this->y = y;
	}

	void setX(int x)
	{
		this->x = x;
	}

	void setY(int y)
	{
		this->y = y;
	}

	int getX()
	{
		return this->x;
	}

	int getY()
	{
		return this->y;
	}

	/*
	 * 重载减号运算符
	 * 重载运算符使用operator关键字
	 * 为什么要是用引用作为参数，是为了防止重复创建对象（调用拷贝构造函数）
	 * const 关键常量，为了防止内部去修改引用的值
	 * */
	Vector operator - (const Vector &vector)
	{
		int x = this->x - vector.x;
		int y = this->y - vector.y;
		Vector vec(x, y);
		return vec; // 不建议返回引用，因为当该函数调用完毕，函数内的对象会被析构函数进行回收
	}

	// 重载加号运算符
	Vector operator + (const Vector &vector)
	{
		int x = this->x + vector.x;
		int y = this->y + vector.y;
		Vector vec(x, y);
		return vec;
	}

	// ++X
	void operator ++ ()
	{
		this->x = this->x++;
	}

	// X++
	void operator ++ (int)
	{
		this->x = this->x++;
	}

	// 重载输出运算符
	friend ostream & operator << (ostream &osStr, const Vector &vec)
	{
		osStr << vec.x << "," << vec.y << endl;
		return osStr;
	}

	// 重载条件运算符
	bool operator == (const Vector &vector)
	{
		return (this->x == vector.x) && (this->y == vector.y);
	}

};

class Array {
private:
	int size;
	int *array;
public:
	void set(int index, int value)
	{
		this->array[index] = value;
	}

	int get(int index)
	{
		return array[index];
	}

	int getSize()
	{
		return this->size;
	}
public:
	Array(int size)
	{
		cout << "Array构造函数" << endl;
		this->size = size;
		this->array = (int *)malloc(sizeof(int)*size);
	}
	
	~Array()
	{
		cout << "Array析构函数" << endl;
		if(this->array)
		{
			free(this->array);
			this->array = NULL;
		}
	}

	Array(const Array &array)
	{
		cout << "Array拷贝构造函数" << endl;
		this->size = array.size;
		this->array = (int *)malloc(sizeof(int)*array.size);
		for (int i = 0; i < array.size; i++) {
			this->array[i] = array.array[i];
		}
	}

	// 重载下标操作符[]
	int operator [] (int index)
	{
		return this->array[index];
	}

};

void printArray(Array array)
{
	
	for (int i = 0; i < array.getSize(); i++) {
		//cout << array.get(i) << endl;
		// 通过重载操作符[]的方式，获取对应下标的值
		cout << array[i] << endl;
	}
}

int main(int argc, char** args)
{

	Vector vec1(3,9);
	Vector vec2(4,7);

	Vector vec3 = vec1 + vec2;
	Vector vec4 = vec1 - vec2;

	cout << "vec3.x:" << vec3.getX() << ", vec3.y:" << vec3.getY() << endl; 
	cout << "vec4.x:" << vec4.getX() << ", vec4.y:" << vec4.getY() << endl; 
	
	vec3++;
	cout << "vec3++" << vec3.getX() << endl;

	++vec4;
	cout << "++vec4" << vec4.getX() << endl;

	cout << vec4;

	bool ret = vec1 == vec2;
	cout << "vec1 == vec2 : " << ret << endl;

	Array *array = new Array(5);
	array->set(0, 6);
	array->set(1, 16);
	array->set(2, 26);

	printArray(*array);

	return 0;
}
